<template>
  <a-modal
    :title="title"
    :width="800"
    :bodyStyle="bodyStyle"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:15%;height: 70%;overflow-y: hidden"
  >
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel">
        关闭
      </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" id="customerModal">
        <a-col :span="12">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="内部编码">
            <a-input disabled placeholder="请输入内部编码" v-decorator.trim="['contractCode']" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="总金额">
            <a-input placeholder="请输入总金额" v-decorator.trim="['contractAmount', validatorRules.name]" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="修改批注" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入批注" v-decorator.trim="['amountModifyRemark', validatorRules.name]" />
          </a-form-item>
        </a-col>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
import pick from 'lodash.pick'
import { postAction } from '@/api/manage'
import { AddInstall, checkSupplier } from '@/api/api'
import { autoJumpNextInput } from '@/utils/util'
export default {
  name: 'EditAmount',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      record: null,
      PraductNameList: [],
      isReadOnly: false,
      bodyStyle: {
        padding: '20',
        height: window.innerHeight * 0.5 + 'px',
        'overflow-y': 'auto'
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        name: {
          rules: [{ required: true, message: '请输入!' }]
        }
      }
    }
  },
  mounted() {},
  methods: {
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'contractCode', 'contractAmount', 'amountModifyRemark'))
        autoJumpNextInput('customerModal')
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let formData = Object.assign(values)
          if (this.model.beginNeedGet && this.model.beginNeedPay) {
            that.$message.warn('期初应收和期初应付不能同时输入')
            that.confirmLoading = false
            return
          }
          formData.contractCode = this.model.contractCode
          formData.id = this.model.id
          let params = {
            contractCode: formData.contractCode,
            contractAmount: formData.contractAmount,
            amountModifyRemark: formData.amountModifyRemark,
            id: formData.id
          }
          postAction('/contractdetails/modifyAmount', params)
            .then(res => {
              if (res.code === 200 || res.code === 10000) {
                that.$message.success(res.message)
                that.$emit('ok')
                console.log(res)
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>
<style scoped></style>
